import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

const MaterialColor PrimaryColor = MaterialColor(0xFFF48FB2, <int, Color>{
  50: Color(0x0DF48FB2),
  100: Color(0x1AF48FB2),
  200: Color(0x33F48FB2),
  300: Color(0x4DF48FB2),
  400: Color(0x66F48FB2),
  500: Color(0x80F48FB2),
  600: Color(0x99F48FB2),
  700: Color(0xB3F48FB2),
  800: Color(0xCCF48FB2),
  900: Color(0xE6F48FB2),
});

class ColorSystem {
  /// 主色调
  final MaterialColor primaryColor;
  /// 次主色调
  final Color secondaryColor;
  /// 警告色 - 橙黄色代表警告、金钱
  final Color warningColor;
  /// 成功色 - 绿色代表成功
  final Color successColor;
  /// 失败色 - 红色代表失败
  final Color failColor;
  /// 文本颜色 - 前景色与背景色对立，一般去背景色的反色
  final Color primaryForeColor;
  /// 背景色 - 背景色与前景色对立，一般去前景色的反色
  final Color primaryBackColor;
  /// 链接颜色 - 蓝色代表进度条、链接的颜色
  final Color linkColor;
  /// 是否是暗色主题
  final Brightness brightness;
  /// 阳色
  final Color maleColor;
  /// 阴色
  final Color femaleColor;
  /// 扩展颜色样式
  final dynamic extendColorSystem;

  ColorSystem(
    {
      this.extendColorSystem,
      this.primaryColor = PrimaryColor,
      this.secondaryColor = const  Color(0xFF007AFF),
      this.warningColor = const  Color(0xFFFF7B00),
      this.successColor = const Color(0xFF4CDA64),
      this.failColor = const Color(0xFFFF2038),
      this.primaryForeColor = const Color(0xFF000000),
      this.primaryBackColor = const Color(0xFFFFFFFF),
      this.linkColor = const Color(0xFF007AFF),
      this.brightness = Brightness.light,
      this.maleColor = const  Color(0xFF007AFF),
      this.femaleColor = const Color(0xFFFF4F88),
      ForeColorSystem foreColorSystem,
      BackColorSystem backColorSystem,
      ButtonColorSystem buttonColorSystem,
    }
  ) {
    this.foreColorSystem = foreColorSystem ?? ForeColorSystem.fromForeColor(primaryForeColor);
    this.backColorSystem = backColorSystem ?? BackColorSystem.fromBackColor(primaryBackColor);
    this.buttonColorSystem = buttonColorSystem ?? ButtonColorSystem.withColorSystem(
      primaryForeColor: primaryForeColor,
      primaryBackColor: primaryBackColor,
      primaryColor: primaryColor,
    );
    this.darkForeColorSystem = brightness == Brightness.dark ? this.foreColorSystem :  ForeColorSystem.fromColors(
      primaryColor: invertColor(this.foreColorSystem.primaryColor),
      secondaryColor: invertColor(this.foreColorSystem.secondaryColor),
      placeholderColor: invertColor(this.foreColorSystem.placeholderColor),
      borderColor: invertColor(this.foreColorSystem.borderColor),
    );
  }
  /// 以下是计算色
  ForeColorSystem foreColorSystem;
  ForeColorSystem darkForeColorSystem;
  BackColorSystem backColorSystem;
  ButtonColorSystem buttonColorSystem;

  static Color invertColor(Color color) {
    return Color.fromARGB(color.alpha, 255 - color.red, 255 - color.green, 255 - color.blue);
  }

  static Color blendModeColor(Color a, Color b, double right) {
    int alpha = (a.alpha*right+b.alpha*(1-right)).toInt();
    int red = (a.red*right+b.red*(1-right)).toInt();
    int green = (a.green*right+b.green*(1-right)).toInt();
    int blue = (a.blue*right+b.blue*(1-right)).toInt();
    return Color.fromARGB(alpha, red, green, blue);
  }

  Color foreColorWithDarkBack(Color color) {
    return brightness == Brightness.dark ? color : invertColor(color);
  }

  ThemeData get themeData {
    return ThemeData(
      primarySwatch: primaryColor,
      brightness: brightness,
      scaffoldBackgroundColor:  primaryBackColor,
      accentColor: foreColorSystem.placeholderColor,
      appBarTheme: AppBarTheme(
        brightness: brightness,
      )
    );
  }
}

/// 前景色颜色系统
class ForeColorSystem {
  ForeColorSystem.fromForeColor(Color primaryForeColor) {
    this.primaryColor = primaryForeColor;
    this.secondaryColor = primaryForeColor.withOpacity(0.6);
    this.placeholderColor = primaryForeColor.withOpacity(0.3);
    this.borderColor = primaryForeColor.withOpacity(0.1);
  }

  ForeColorSystem.fromColors({
    this.primaryColor,
    this.secondaryColor,
    this.placeholderColor,
    this.borderColor,
  });
  
  /// 100%（用于主文字色）
  Color primaryColor;
  /// 60%（用于次文字色）
  Color secondaryColor;
  /// 30%（用于占位符文字提示、按钮的禁用状态）
  Color placeholderColor;
  /// 10%（用于分隔线、forms边框）
  Color borderColor;
}

/// 背景色颜色系统
class BackColorSystem {
  BackColorSystem.fromBackColor(Color primaryBackColor) {
    this.primaryColor = primaryBackColor;
    var invertColor = ColorSystem.invertColor(primaryBackColor);
    this.secondaryColor = ColorSystem.blendModeColor(primaryBackColor, invertColor, 0.9) ;
  }

  BackColorSystem.fromColors({
    this.primaryColor,
    this.secondaryColor,
  });
  
  
  /// 100%（用于背景色）
  Color primaryColor;
  /// 90%（用于次背景色）
  Color secondaryColor;
}

/// 按钮颜色系统
class ButtonColorSystem {

  ButtonColorSystem.withColorSystem({
    Color primaryForeColor,
    Color primaryColor,
    Color primaryBackColor,
  }) {
    this.baseColor = primaryForeColor;
    this.backColor = primaryBackColor;
    this.activeColor = primaryColor;
    this.touchDownColor = primaryColor.withOpacity(0.9);
    this.loadingColor = primaryColor.withOpacity(0.9);
    this.inactiveColor = primaryColor.withOpacity(0.3);
  }

  /// 按钮底色层
  Color baseColor;
  /// 默认背景色
  Color backColor;
  /// 激活状态下背景色
  Color activeColor;
  /// 按下状态下背景色
  Color touchDownColor;
  /// 加载状态下颜色
  Color loadingColor;
  /// 禁用状态下的颜色
  Color inactiveColor;
}

class CYThemeData extends InheritedWidget {
  final ColorSystem colorSystem;

  CYThemeData({
    this.colorSystem,
    Widget child,
  }) : super(child: DefaultTextStyle(
    style: TextStyle( 
      color: colorSystem.primaryForeColor, 
      inherit: true, 
      fontWeight: FontWeight.w400
    ),
    child: child
  ));

  static EventBus eventBus = EventBus();

  static ColorSystem of(BuildContext context){
    CYThemeData colorTheme = context.dependOnInheritedWidgetOfExactType<CYThemeData>();
    return colorTheme.colorSystem;
  }

  static changeColorSystem(ColorSystem cs) {
    final SystemUiOverlayStyle overlayStyle = cs.brightness == Brightness.dark
      ? SystemUiOverlayStyle.light
      : SystemUiOverlayStyle.dark;
    SystemChrome.setSystemUIOverlayStyle(overlayStyle);
    eventBus.fire(cs);
  }

  @override
  bool updateShouldNotify(InheritedWidget oldWidget) {
    return true;
  }
}

/// 暗色风格
class DarkColorSystem extends ColorSystem {
  DarkColorSystem({
    Color primaryForeColor = const Color(0xFFFFFFFF), 
    Color primaryBackColor = const Color(0xFF000000),
    Color linkColor = const Color(0xFF007AFF),
    ForeColorSystem foreColorSystem,
    BackColorSystem backColorSystem,
    ButtonColorSystem buttonColorSystem,
  }) : super(
    primaryForeColor: primaryForeColor, 
    primaryBackColor: primaryBackColor,
    brightness: Brightness.dark,
    linkColor: linkColor,
    foreColorSystem: foreColorSystem,
    backColorSystem: backColorSystem,
    buttonColorSystem: buttonColorSystem,
  );
}

WidgetBuilder cyThemeWrapBuilder (WidgetBuilder builder, BuildContext context) {
  final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
  final ColorSystem colorSystem = CYThemeData.of(context);
  return (BuildContext _){
    Widget pageChild = builder(_);
      if(theme != null) {
        pageChild =  Theme(data: theme, child: pageChild);
      }
      if(colorSystem != null) {
        pageChild =  CYThemeData(colorSystem: colorSystem, child: pageChild);
      }
      return pageChild;
  };
}

